package com.huatai.datadevelop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huatai.common.api.R;
import com.huatai.common.error.exception.ServiceException;
import com.huatai.datadevelop.entity.RealApiManageEntity;
import com.huatai.datadevelop.entity.RealApiVonversionEntity;
import com.huatai.datadevelop.service.RealApiManageService;
import com.huatai.datadevelop.utils.PageUtils;
import com.huatai.datadevelop.vo.DeleteVO;
import com.huatai.datadevelop.vo.NameComplianceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hecai
 * @description: TODO  实时数据接口配置
 * @date 2022/11/29 10:55
 * @Version 1.0
 */
@Api(value = "实时数据接口配置",tags = "实时数据接口配置")
@RestController
@RequestMapping("api/real/manage")
public class RealController {

	@Autowired
	private RealApiManageService realApiManageService;

	/**
	 * 获取新增的接口英文名称是否合规
	 *
	 * @return
	 */
	@ApiOperation("获取新增的接口英文名称是否合规")
	@PostMapping("/getInterfaceNameCompliance")
	public R getInterfaceNameCompliance(@RequestBody NameComplianceVO data) {
		return R.data(this.realApiManageService.getInterfaceNameCompliance(data.getInterfaceName()));
	}

	/**
	 * 获取列表
	 */
	@ApiOperation("获取列表")
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		PageUtils page = realApiManageService.queryPage(params);
		return R.data(page);
	}

	/**
	 * 新增或更新
	 * @return
	 */
	@ApiOperation("新增或更新")
	@PostMapping("/saveOrUpdate")
	public R saveOrUpdate(@RequestBody RealApiManageEntity data) {
		Integer id = data.getId();
		if (id == null) {
			String interfaceName = data.getInterfaceName();
			HashMap<String, Object> interfaceNameCompliance = this.realApiManageService.getInterfaceNameCompliance(interfaceName);
			Boolean o = (Boolean) interfaceNameCompliance.get("success");
			if (!o) {
				throw new ServiceException("英文名称重复");
			}
		}

		data.setBody(JSON.toJSONString(data.getJsonBody()));
		data.setHeard(JSON.toJSONString(data.getJsonHeard()));
		data.setKeyReplaceValue(JSON.toJSONString(data.getJsonKeyReplaceValue()));
		//此处还差逻辑处理
		return R.data(this.realApiManageService.saveOrUpdateManage(data));
	}

	/**
	 * 删除或批量删除
	 * @return
	 */
	@ApiOperation("删除或批量删除")
	@PostMapping("/deleteByIds")
	public R deleteByIds(@RequestBody DeleteVO data) {
		return R.data(this.realApiManageService.removeByIds(data.getIds()));
	}

	/**
	 * TODO 前端大屏调用接口
	 * 调取数据
	 *
	 * @return
	 */
	@ApiOperation("前端大屏调用接口")
	@PostMapping("/asyncRequestApi/{interfaceName}")
	public R asyncRequestApi(@RequestBody JSONObject data,
							 @PathVariable("interfaceName") String interfaceName) {
		RealApiManageEntity apiManageEntity = this.realApiManageService.getOne(new QueryWrapper<RealApiManageEntity>().eq("interface_name", interfaceName));
		if (apiManageEntity == null) {
			throw new ServiceException("没有" + interfaceName + "这个接口的配置，请核查");
		}
		Object object = this.realApiManageService.asyncRequestApi(data, apiManageEntity);
		return R.data(object);
	}

	/**
	 * 获取第三方接口请求数据类型
	 *
	 * @return
	 */
	@ApiOperation("获取第三方接口请求数据类型")
	@PostMapping("/getRequestDataTypeList")
	public R getRequestDataTypeList() {
		return R.data(Arrays.asList("ArrayList", "LinkedHashMap", "String"));
	}

	/**
	 * 获取现有配置数据的值域
	 *
	 * @return
	 */
	@ApiOperation("获取现有配置数据的值域")
	@PostMapping("/getConversionKey")
	public R getConversionKey(@RequestBody JSONObject data) {
		return R.data( this.realApiManageService.getConversionKey(data.getString("key")));
	}

	/**
	 * 保存配置
	 *
	 * @return
	 */
	@ApiOperation("保存配置")
	@PostMapping("/saveOrUpdateConversion")
	public R saveOrUpdateConversion(@RequestBody JSONObject data) {
		JSONArray dataList = data.getJSONArray("dataList");
		List<RealApiVonversionEntity> entity = JSON.parseArray(JSON.toJSONString(dataList), RealApiVonversionEntity.class);
		if (entity != null && entity.size() > 0) {
			String realapiId = entity.get(0).getRealapiId();
			this.realApiManageService.saveOrUpdateConversion(entity, realapiId);
		}
		return R.success("处理成功");
	}

	/**
	 * 获取配置数据
	 *
	 * @return
	 */
	@ApiOperation("获取配置数据")
	@PostMapping("/getConversionList")
	public R getConversionList(@RequestBody JSONObject data) {
		return R.data( this.realApiManageService.getConversionList(data.getString("realapiId")));
	}
}
